<?php

namespace app\api\controller;

use app\api\validate\StorageValidate;
use think\Controller;
use think\Request;
use app\api\model\StorageModel;
use think\Db;
use think\Validate;

class Storage extends Common
{
    /**
     * 入库单  列表
     * @return string|\think\response\Json
     * @throws \think\exception\DbException
     */
    public function index(Request $request, StorageModel $model)
    {
        $data =$request->param();
        $data['start_time'] =isset($data['start_time'])?strtotime($data['start_time']):"";
        $data['end_time'] =isset($data['end_time'])?strtotime($data['end_time']):"";
        $data['number'] =isset($data['number'])?$data['number']:"";
        $list = $model->getOutList($data);
        if (!empty($list)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $list]);
        } else {
            return json(['code' => 404, 'msg' => '失败']);
        }
    }

    /**
     * 入库单 详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($id,StorageModel $model)
    {
        $info = $model->getInfo($id);
        if (!empty($info)) {
            return json(['code' => 200, 'msg' => '成功', 'list' => $info]);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }

    public function depot($id,Request $request,StorageModel $model)
    {

        try {
            $option =$request->param('goods');
            $list = $model->getDepot($id, $option);
            if ($list == 1) {
                return json(['code' => 200, 'msg' => '成功']);
            } else {
                return json(['code' => 500, 'msg' => '输入数量超出库存上限']);
            }
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage()], 500);
        }
    }
    /**
     * 入库单 撤销
     * @return \think\response\Json
     */
    public function revoke($id,StorageModel $model)
    {
        $arr = $model->getRevoke($id);
        if ($arr ==1) {
            return json(['code' => 200, 'msg' => '成功']);
        } else {
            return json(['code' => 500, 'msg' => '失败']);
        }
    }
}
